﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace UpLoad
{
    public class MusicUpLoad : UpLoadBase
    {
        
        
        /// <summary>
        /// 重写父类的上传音频方法
        /// </summary>
        /// <param name="uploadfile"></param>
        /// <returns></returns>
        public override ReturnModel SaveFile(System.Web.UI.WebControls.FileUpload uploadfile)
        {
            ReturnModel model = new ReturnModel();
            string suffix = GetSuffix(uploadfile.FileName);         //获取扩展名
            string filename = GetOnlyName(suffix);                  //获取生成的文件名
            string uploadPath = Config.MusicUploadPath.Trim() == "" ? Config.DefaultPath : Config.MusicUploadPath;      //获取上传路径
            string fullname = Path.Combine(uploadPath, filename);          //获取完整的上传路径和文件名
            if (!Config.musicsuffixs.Contains(suffix))        //判断是否为上传类型
            {
                model.IsSuccess = false;
            }
            else if (UpLoad(uploadfile, fullname))              //判断是否上传成功
            {
                FileInfoModel info = new FileInfoModel();
                info.filename = filename;
                info.filetype = GetFileType(suffix);
                info.createtime = DateTime.Now;
                info.updatetime = DateTime.Now;
                model.FileInfo = info;
                if(new FileDal().AddFile(model.FileInfo))
                    model.IsSuccess = true;
            }
            else
            {
                model.IsSuccess = false;
            }
            return model;
        }


        /// <summary>
        /// 获取服务器的完整文件名称
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public override string GetServerPath(string filename)
        {
            string filepath = Config.MusicServerPath.Trim() == "" ? Config.DefaultServerPath : Config.MusicServerPath;
            return Path.Combine(filepath, filename).ToString();
        }
        

        /// <summary>
        /// 删除音频
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public override bool DeleteRecord(string filename)
        {
            string filepath = Config.MusicServerPath.Trim() == "" ? Config.DefaultServerPath : Config.MusicServerPath;
            return RemoveFile(filepath, filename);
        }

        /// <summary>
        /// 修改文件
        /// </summary>
        /// <param name="fileupload"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public override ReturnModel UpdateFile(System.Web.UI.WebControls.FileUpload fileupload, string filename)
        {
            string uploadPath = Config.MusicUploadPath.Trim() == "" ? Config.DefaultPath : Config.MusicUploadPath;      //获取上传路径
            string filepath = Config.MusicServerPath.Trim() == "" ? Config.DefaultServerPath : Config.MusicServerPath;
            return ModifyFile(fileupload, filename, uploadPath, filepath);
        }
    }
}
